function content0LoadFunction() {
	if ($('resourceList').innerHTML == '') {
		// If resourceList div is empty, populate with new resources
		getNewResources();	
	}	
	
	if ($('resourceTree').innerHTML == '') {
		// If resourceTree div is empty, then populate with resource tree
		createResourceTree();	
	}
}	

function content2LoadFunction() {
	// Get broken links
	x_getBadLinks(returnGetBadLinks);
}

function returnGetBadLinks(string) {
	var stringArray = string.split('|');
	var badLinks  = stringArray[0];
	var noResults = stringArray[1];
	
	// Set panel title
	panel2.setTitle('Broken Links ('+noResults+')');	
	$('badLinksList').innerHTML = badLinks;
}

function getNewResources() {
	x_getNewResources(returnGetResources);
}

function getResources(levelID,levelNumber,uploadCheck) {
	x_getResources(levelID,levelNumber,returnGetResources);
}

function returnGetResources(string) {
	var stringArray = string.split('|');
	var breadCrumbs = stringArray[0];
	var content     = stringArray[1];
	var pathID      = stringArray[2];
	
	// Force to content0 if we're not there already
	//Example.toggleContent('content0','','');
	
	$('resourceList').innerHTML = content;
	$('resourcePath').innerHTML = '<h3>'+breadCrumbs+'</h3>';
	$('pathID').value = pathID;
}

function editResource(linkID) {
	x_getResourceForEdit(linkID,returnGetResourceForEdit);
}

function returnGetResourceForEdit(string) {
	var stringArray = string.split('|');
	
	var lTitle = stringArray[0];
	var lDesc  = stringArray[1];
	var lURL   = stringArray[2];
	var lPaths = stringArray[3];	
	var linkID = stringArray[4];
		
	$('associatedCatContainer').innerHTML = lPaths;
	$('pTitleBox').value = lTitle;
	$('pURL').value      = lURL;
	if (lDesc == '') {
		lDesc = ' ';
	}
	tinyMCE.getInstanceById('pShortDesc').getBody().innerHTML = lDesc;
	
	// Turn off add button
	$('buttonNew').style.display = 'none';
	
	// Set formAction to 'edit'
	$('formAction').value = 'edit';
	$('linkID').value = linkID;
	
	// Populate category drop-downs
	getLevel(0,0,0);
	
	// Switch to panel 0 (we could be coming here from panel 1: search or panel 2: bad links)
	Example.toggleContent(0,'','');
	
	// Display add form
	new Effect.SlideDown('formNewResource', {direction: 'top', duration: .3});
}

function addResource() {
	// Clear orphaned categories
	removeOrphanedCats();
	
	// Turn off add button
	$('buttonNew').style.display = 'none';
	
	// Set hidden field defaults
	$('formAction').value = 'add';
	$('linkID').value = '0';
	
	// Populate category drop-downs
	getLevel(0,0,0);
		
	// Display add form
	new Effect.SlideDown('formNewResource', {direction: 'top', duration: .3});		
}

function saveResource() {
	var linkID = $('linkID').value;
	if (linkID == '') {
		linkID = 0;
	}
	var formAction = $('formAction').value;
	var lTitle     = $('pTitleBox').value;
	var lURL       = $('pURL').value;
	var lShortDesc = tinyMCE.getContent('pShortDesc');
	
	var phpString  = lTitle+'|'+lURL+'|'+lShortDesc;
	if (lTitle == '') {
		// Error
		var message = 'You must enter a link title.';
		updateStatus(message,'error','statusDiv');
	} else {
		x_saveResource(phpString,formAction,linkID,returnSaveResource);	
		
		// Close add window
		cancelResource();
		
		// Update status message
		if (formAction == 'add') { var message = 'Your resource has been added.'; }
		else if (formAction == 'edit') { var message = 'Your link has been edited.'; }
		updateStatus(message,'success','statusDiv');
	}	
}

function returnSaveResource() {
	var levelID = $('pathID').value;
	getResources(levelID,0,0);
}

function cancelResource() {
	// Display add form
	new Effect.SlideUp('formNewResource', {direction: 'top', duration: .2});
	
	// Clear all form elements
	$('pTitleBox').value = '';
	$('pURL').value      = '';
	//tinyMCE.getInstanceById('pShortDesc');	
	//tinyMCE.setContent('');
		
	$('associatedCatContainer').innerHTML = '';
	$('pathSave').style.display = 'none';	
	
	// Turn on add button
	$('buttonNew').style.display = 'block';	
}

function getLevel(path,level,parentNode) {
	x_getLevelSelects(path,level,parentNode,returnGetLevel);
}

function returnGetLevel(string) {
	var stringArray = string.split('|');
	var levelSelect = stringArray[0];
	var status      = stringArray[1];
	var levelNo     = stringArray[2];
	var parentNode  = stringArray[3];
	
	$('selectParent').innerHTML = levelSelect;
	if (levelNo == 2) {
		// Turn on Save button
		$('pathSave').style.display = 'block';
	}	
}

function returnSubmitLink(string) {
	var stringArray = string.split('|');
	var levelSelect = stringArray[0];
	var status      = stringArray[1];
	document.getElementById('actionStatus').innerHTML = '<div style="padding-top: 3px; padding-bottom: 3px;" class="successMessage">'+status+'</div>';
}

function toggleLinkStatus(linkID,status) {
	x_toggleLinkStatus(linkID,status,returnToggleLinkStatus);
}

function returnToggleLinkStatus(string) {
	var stringArray = string.split('|'); 
	var linkID      = stringArray[0];
	var newStatus   = stringArray[1];
	var toggleTD = 'linkStatus'+linkID;
	if (newStatus == 1) {
		var linkHTML = '<img style="cursor: pointer;" onClick="toggleLinkStatus('+linkID+','+newStatus+'); return false;" src="images/iconYes.gif" title="Link is active: click to make inactive">';
	} else {
		newStatus = 0;
		var linkHTML = '<img style="cursor: pointer;" onClick="toggleLinkStatus('+linkID+','+newStatus+'); return false;" src="images/iconNo.gif" title="Link is inactive: click to make active">';
	}
	document.getElementById(toggleTD).innerHTML = linkHTML;
}

function returnConfirmDelete(linkID) {
	$('row'+linkID).style.display = 'none';
}

function returnEditLinkConfirm(string) {
	// Re-populate the table row with new data
	var stringArray = string.split('|');
	var linkID    = stringArray[0];
	var linkTitle = stringArray[1];
	var linkDesc  = stringArray[2];
	var linkURL   = stringArray[3];
	
	$('controlCell'+linkID).style.background = '#ccff00';
	$('linkURL'+linkID).innerHTML = '<a href="'+linkURL+'" target="_blank" title="View '+linkTitle+'"><b>'+linkTitle+'</b></a>';
	$('linkDesc'+linkID).innerHTML = linkDesc;
	$('actionStatus').innerHTML = '<div class="successMessage">This resource has been updated.</div>';
}

function addResourceCat() {
	var linkID    = $('linkID').value;
	var mainCat   = $('mainCat').options;
	var subCat    = $('subCat').options;
	var subSubCat = $('subSubCat').options;
		
	var mainCat   = mainCat[mainCat.selectedIndex].value;
	var subCat    = subCat[subCat.selectedIndex].value;
	var subSubCat = subSubCat[subSubCat.selectedIndex].value;
	
	x_addResourceCat(mainCat,subCat,subSubCat,linkID,returnAddResourceCat);
}

function returnAddResourceCat(string) {
	var stringArray = string.split('|');
	var catString = stringArray[0];
	var pathID    = stringArray[1];
	var pathString= stringArray[2];
	var linkID    = stringArray[3]; // <-- Need to pass this in
	
	var string = '<a href="#" onClick="removeResourceCat('+pathID+','+linkID+'); return false;" title="Remove this category">';
	string    += '<img src="images/iconError.gif" style="border: none;" alt="Remove this category" /></a>&nbsp;';
	string    += '<a href="http://www.tcntransition.org'+pathString+'" target="_blank">'+catString+'</a> ';
	
	var associatedCatContainer = $('associatedCatContainer');
	var newCatDiv = document.createElement('div');
	
	newCatDiv.id = 'path'+pathID;
	newCatDiv.style.height = '25px';
	
	newCatDiv.innerHTML = string;
			
	associatedCatContainer.appendChild(newCatDiv);
}

function removeResourceCat(pathID,linkID) {
	// Turn off the cat div
	$('path'+pathID).style.display = 'none';
	x_removeResourceCat(pathID,linkID,returnRemoveResourceCat);	
}

function returnRemoveResourceCat() {
	// Do nothing
}

function removeOrphanedCats() {
	x_removeOrphanedCats(returnRemoveOrphanedCats);	
}

function returnRemoveOrphanedCats() {
	//  Do nothing 
}

//***************************************************************************
function linkCheck() {
	x_linkCheck(returnLinkCheck);
}

function returnLinkCheck(string) {
	var stringArray = string.split('|');
	var runDate  = stringArray[0];
	var runBy    = stringArray[1];
	var noLinks  = stringArray[2];
	
	var content = '<div class="popupWindow">';
	content    += 'Link Checker was last run on '+runDate;
	content    += '<a href="#" onClick="linkCheckConfirm(0,'+noLinks+'); return false;"><br /><b>Run Link Check Now on '+noLinks+' Links</b></a>';
	content    += '<div id="progress" style="float: left;" class="linkCheckProgress"></div><div id="percent" class="percentBox" style="float: left;"></div>';
	content    += '<div style="clear: left;"></div></div>';
		
	document.getElementById('resourcePath').innerHTML = '<h2>Check Links</h2>';
	document.getElementById('content2').innerHTML = content
}

function linkCheckConfirm(linkStart,totalLinks) {
	x_linkCheckConfirm(linkStart,totalLinks,returnLinkCheckConfirm);
}

function returnLinkCheckConfirm(string) {
	var stringArray = string.split('|');
	var linkStart   = stringArray[0];
	var totalLinks  = stringArray[1];
	var percent     = stringArray[2];
	var barSize     = stringArray[3];
	var badLinks    = stringArray[4];
	document.getElementById('percent').innerHTML = linkStart+' '+percent+'%'; 
	document.getElementById('progress').innerHTML = '<img src="images/barProgress.gif" style="height: 25px; width: '+barSize+'px;">'; 
	document.getElementById('badLinks').innerHTML = badLinks;
	x_linkCheckConfirm(linkStart,totalLinks,returnLinkCheckConfirm);
	
}
